package com.order.controller;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.order.Utils;
import com.order.dao.CustomerDao;
import com.order.pojo.Customer;
import com.order.service.CustomerService;
import jdk.nashorn.internal.runtime.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@Controller
@Controller
@RequestMapping("customer")
public class CustomerController {
    @Autowired
   // private CustomerDao customerDao;
    private CustomerService customerService;

   /* @RequestMapping("/login")
    @ResponseBody
    public Map<Object,Object> test(String phone, String password)
    {
        Map<Object,Object> map=new HashMap<>();
        Integer login = dao.login(phone, password);
        if(login!=null)
        {
            map.put("code",200);
            map.put("msg","登录成功,id为"+login);
        }
        else
        {
            map.put("code",500);
            map.put("msg","登陆失败,用户名或密码错误");
        }
        //map.put("data","{}");
       return map;
    }*/
   @RequestMapping("/login")
   @ResponseBody
   public Customer login(String phone, String password)
   {
       return customerService.login(phone,password);
   }
    @RequestMapping("/setPass")
    @ResponseBody
    public String pass(String phone,String oldPass,String newPass)
    {
       // return dao.updatePassword(phone, oldPass, newPass);
        Boolean flag = customerService.updatePassword(phone, oldPass, newPass);
        if(flag)
            return "修改成功";
        else
            return "修改失败";

    }

    @RequestMapping("/recharge")
    @ResponseBody
    public String recharge(Integer id,Double money)
    {
        Boolean flag = customerService.recharge(id, money);
        if(flag)
            return "充值成功"+money+"元";
        else
            return "充值失败,请联系管理员";

    }

    @RequestMapping("/findAll")
    @ResponseBody
    public List<Customer> findAll()
    {
        return customerService.findAllCustomers();
    }

    @RequestMapping("/test")
    @ResponseBody
    //去掉末尾的逗号
    public String test(String str)
    {
        /*if(str.endsWith(","))
            return str.substring(0,str.length()-1);
        else
            return str;*/
        //封装的工具类中的trim方法对空指针、空字符情况进行了处理
        return Utils.trim(str);
    }

    //注册
    @RequestMapping("/submit")
    @ResponseBody
    public Boolean submit(Customer customer)
    {
        return customerService.submit(customer);
    }
    //修改爱好和忌口
    @RequestMapping("/set")
    @ResponseBody
    public Boolean set(Customer customer)
    {
        return customerService.updateFlavorAndTaboo(customer);
    }



}
